package com.cssw.fyzb.busi.api.adnumprice;

import com.cssw.fyzb.busi.service.adnumprice.AdNumPriceService;
import com.ydn.appserver.Action;
import com.ydn.appserver.Request;
import com.ydn.appserver.Response;
import com.ydn.appserver.annotations.Function;
import com.ydn.appserver.annotations.Parameter;
import com.ydn.appserver.annotations.Type;
import com.ydn.simplelock.SimpleLock;
import com.ydn.simplelock.SimpleLockFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author
 */
@Function(description = "领取任务", parameters = {
        @Parameter(name = "userId", type = Type.String, description = "用户标识", required = true)
})
@Component
public class LookAd implements Action {

    private final static Logger logger = LoggerFactory.getLogger(com.cssw.fyzb.busi.api.task.CollecTask.class);

    @Autowired
    private AdNumPriceService adNumPriceService;

    @Autowired
    private SimpleLockFactory simpleLockFactory;

    @Override
    public Response execute(Request request) throws Exception {
        Long userId = request.getLong("userId");
        SimpleLock lock = simpleLockFactory.getLock("COLL_LOOK_AD" + userId);
        try {
            lock.acquire();
            Boolean err = adNumPriceService.lookAd(userId);
            return Response.success().put("data", err);
        } catch (Exception e) {
            logger.error("", e);
            return Response.fail("完成任务失败");
        } finally {
            lock.release();
        }
    }
}
